//
// Created by mabinjie on 2024/10/29.
//

#ifndef PRETRANSFER_SEGMENTATION_HPP
#define PRETRANSFER_SEGMENTATION_HPP
#include <cstdint>

#include "common/types.h"
#include "protocol/message/jtt808_body.hpp"

namespace protocol::platform {

class RetransferSegmentation final : public jtt808_body {
public:
    uint16_t original_message_id_{};
    uint16_t message_total_{};
    common::container_t<uint16_t> messages_to_retansfer_;

    IMPLEMENT_SIZEABLE(original_message_id_, message_total_, messages_to_retansfer_)
    IMPLEMENT_TOSTRING(message_id_, original_message_id_, message_total_, messages_to_retansfer_)

    RetransferSegmentation() : jtt808_body(0x8003) {}
    RetransferSegmentation(const uint16_t original_message_id, const uint16_t message_total,
        const common::Container<uint16_t> &messages_to_retansfer) : jtt808_body(0x8003),
        original_message_id_(original_message_id), message_total_(message_total) {
        messages_to_retansfer_ = std::make_shared<common::Container<uint16_t>>(messages_to_retansfer);
    }

    RetransferSegmentation(const RetransferSegmentation &other) = default;

    RetransferSegmentation(RetransferSegmentation &&other) noexcept = default;

    RetransferSegmentation & operator=(const RetransferSegmentation &other) = default;

    RetransferSegmentation & operator=(RetransferSegmentation &&other) noexcept = default;

    ~RetransferSegmentation() override = default;

    void serialize(common::MessagePackWriter &writer) override {
        writer.writeUint(original_message_id_);
        writer.writeUint(message_total_);
        messages_to_retansfer_->serialize(writer);
    }

    void deserialize(common::MessagePackReader &reader) override {
        reader.readUint(original_message_id_);
        reader.readUint(message_total_);
        messages_to_retansfer_ = std::make_shared<common::Container<uint16_t>>(2*message_total_);
        messages_to_retansfer_->deserialize(reader);
    }
};

}

#endif //RETRANSFER_SEGMENTATION_HPP
